/*
 * @Author: Paco
 * @Date:   2017-07-24
 * +----------------------------------------------------------------------
 * | jqadmin [ jq酷打造的一款懒人后台模板 ]
 * | Copyright (c) 2017 http://jqadmin.jqcool.net All rights reserved.
 * | Licensed ( http://jqadmin.jqcool.net/licenses/ )
 * | Author: Paco <admin@jqcool.net>
 * +----------------------------------------------------------------------
 */

layui.define(['jquery', 'jqbind'], function(exports) {
    var $ = layui.jquery,
        layer = layui.layer,
		jqbind = layui.jqbind,
        jqlable = function() {
            this.options = {}
        };

    /**
     * @todo 初始化需要绑定事件的元素
     * 
     */
    jqlable.prototype.init = function() {
        var _this = this;

        $(".lable_button:not([bind])").each(function() {
            jqbind.bind($(this));
			_this.tips(this);
            $(this).bind('click', function() {
                _this.lable(this);
            });
        });


    }


    jqlable.prototype.lable = function(obj) {
        var _this = this,
            input =$(obj).data('input'),
			myValue = $(obj).data('value');
			       
		var $t = $(input)[0];
		
		if (document.selection) {
			this.focus();
			sel = document.selection.createRange();
			sel.text = myValue;
			this.focus();
		} 
		else if ($t.selectionStart || $t.selectionStart == '0') {
				var startPos = $t.selectionStart;
				var endPos = $t.selectionEnd;
				var scrollTop = $t.scrollTop;
				$t.value = $t.value.substring(0, startPos) + myValue + $t.value.substring(endPos, $t.value.length);
				$(input).focus();
				$t.selectionStart = startPos + myValue.length;
				$t.selectionEnd = startPos + myValue.length;
				$t.scrollTop = scrollTop;
		} else {
			$(input).val($(input).val()+myValue);
			$(input).focus();
		}
    }

    jqlable.prototype.tips = function(obj) {
        var _this = this

		if(typeof($(obj).attr("data-title"))!="undefined") 
		{
			  var tipsTitle = $(obj).data('title');
			  $(obj).on('mouseover',function(){
				    layer.tips(tipsTitle, obj, {
						  tips: [1, '#3595CC'],
						  time: 10000
					});
			  })
		}
		
		
		
    }   

    var jqlableBind = new jqlable();
    jqlableBind.init();
    exports('jqlable', jqlableBind);
});